const express = require("express")
const bodyParser = require("body-parser")
const db = require('./lib/db')
const { execSync } = require('child_process');

// 绑定数据库
global.$db = db;

const app = express()

app.use((req, res, next) => {
    res.setHeader("Access-Control-Allow-Origin", '*')
    res.setHeader('Access-Control-Allow-Headers', '*');

    if (req.method === "OPTIONS") {
        return res.sendStatus(200);
    } else {
        next()
    }

})

app.use(express.static('public'))

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))

// 注册路由
app.use(require("./routers/index"))



// const port = 12345;

let server = app.listen(7788, () => {

    let port = server.address().port;

    console.log(`服务器运行于：http://localhost:${port}`);
    // console.log("不要关闭本窗口！！！");
    // console.log("不要关闭本窗口！！！");
    // console.log("不要关闭本窗口！！！");
    // execSync(`start http://localhost:${port}`)
});